package com.itic.market.mobile.uc.phone;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.utils.StringHelper;
import com.itic.market.mobile.base.BaseMobileInterface;
import com.itic.market.system.user.persistence.model.MarketUser;


/**
 * 用户绑定手机号码接口类
 * @author 许小宝
 * 2017年07月26日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc/phone")
public class MarketUcPhoneInterface
     extends BaseMobileInterface {
	
	
	/**
     * 用户手机号码发送验证码
     * @return 
     */
    @RequestMapping(value="/send")
    @ResponseBody
    public Map<String,Object> send( HttpServletRequest request, HttpServletResponse response, String token, String phone ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if(  StringHelper.isNotBlank(phone) ){
    			String addResult = sendMassageByHuifa(phone);
				if( SUCCESS.equals(addResult) ){
					result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG; 
				}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
	
    /**
     * 用户绑定手机号码
     * @return 
     */
    @RequestMapping(value="/bind")
    @ResponseBody
    public Map<String,Object> bind( HttpServletRequest request, HttpServletResponse response, String token, String phone, String code ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(phone) && StringHelper.isNotBlank(code) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				//验证手机号的验证码是否正确
    				if( isPhoneCode(phone,code) ){
    					//判断手机号是否已注册
    					MarketUser marketUserByPhone = getMarketUserByPhone(phone);
    					if( null == marketUserByPhone ){
    						//编辑用户的手机号码字段 
        					user.setUserPhone(phone);
        					String editResult = marketUserService.bindPhoneAndDelPhoneCode(user,code);
            				if( SUCCESS.equals(editResult) ){
            					result = SUCCESS;
                    			errorCode = SUCCESS_ERROR_CODE;
                            	errorMsg = SUCCESS_ERROR_MSG; 
            				}
    					}else{
    						result = FAIL;
                			errorCode = PHONE_USED_ERROR_CODE;
                        	errorMsg = PHONE_USED_ERROR_MSG; 
    					}
    					
    				}else{//验证码无效
    					result = FAIL;
            			errorCode = PHONE_CODE_ERROR_CODE;
                    	errorMsg = PHONE_CODE_ERROR_MSG; 
    				}
    				
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * 用户绑定手机号码
     * @return 
     */
    @RequestMapping(value="/get")
    @ResponseBody
    public Map<String,Object> get( HttpServletRequest request, HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				resultMap.put(DATA, user.getUserPhone());
    				result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG; 
    				
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//logException("MallUcPhoneInterface", "bind", e.toString(),"用户绑定手机号码",e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
}